package com.yuyang.wifi.activitys.mine;

import android.os.Handler;
import android.util.Log;

import com.yuyang.wifi.R;
import com.yuyang.wifi.base.BaseActivity;
import com.yuyang.wifi.base.BaseResult;
import com.yuyang.wifi.constant.AppConstant;
import com.yuyang.wifi.model.AdTypeInfoModel;
import com.yuyang.wifi.model.AdTypeInfoResult;
import com.yuyang.wifi.utils.ActivityAllManager;
import com.yuyang.wifi.utils.RxUtil;
import com.yuyang.wifi.utils.Skip;
import com.yuyang.wifi.utils.SpUtil;
import com.yuyang.wifi.utils.ToastUtils;
import com.yuyang.wifi.utils.network.BaseDataSubscriber;
import com.yuyang.wifi.utils.network.HttpErrorHandler;
import com.yuyang.wifi.utils.network.HttpManager;
import com.yuyang.wifi.utils.network.RxDataInstance;
import com.yuyang.wifi.views.LoadCSJiliVideoUtils;
import com.yuyang.wifi.views.LoadCsjNewInsertFullAdUtils;
import com.yuyang.wifi.views.LoadYLHJiliVideoUtils;
import com.yuyang.wifi.views.XTextView;

import org.jcodec.movtool.Remux;

import butterknife.BindView;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NetworkTestSpeedGifActivity extends BaseActivity {

    @BindView(R.id.xt_net_delay_time)
    XTextView xtNetDelayTime;
    @BindView(R.id.xt_download_speed)
    XTextView xtDownloadSpeed;
    @BindView(R.id.xt_upload_speed)
    XTextView xtUploadSpeed;
    @BindView(R.id.gif_cool)
    GifImageView gifCool;
    private LoadCsjNewInsertFullAdUtils newInsertFullAd;
    private LoadYLHJiliVideoUtils loadYLHJiliVideoUtils;
    private int type;

    @Override
    protected void initActionBar() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_network_test_speed_gif;
    }

    @Override
    protected void initContentView() {

        newInsertFullAd = LoadCsjNewInsertFullAdUtils.getInstance();
        newInsertFullAd.load(NetworkTestSpeedGifActivity.this, "csj_jl_network_test");
        newInsertFullAd.setLoadFailureListener(new LoadCsjNewInsertFullAdUtils.OnLoadFailureListener() {
            @Override
            public void onLoadFailureListener() {
                playErrorRepalceAd(2);
            }
        });

        loadYLHJiliVideoUtils = LoadYLHJiliVideoUtils.getInstance();
        loadYLHJiliVideoUtils.load(NetworkTestSpeedGifActivity.this, "ylh_jl_network_test");
        loadYLHJiliVideoUtils.setLoadFailureListener(new LoadYLHJiliVideoUtils.OnLoadFailureListener() {
            @Override
            public void onLoadFailureListener() {
                playErrorRepalceAd(1);
            }
        });

        GifDrawable openRedBagGif = (GifDrawable) gifCool.getDrawable();
        openRedBagGif.start();//开始播放,一直处在播放状态，控制他的显示和隐藏

        showChange();
        getAdTypeInfo();
    }

    private void showChange() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(0.00, 0.00, 0.00);
            }
        }, 300);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(2.22, 23.12, 5.62);
            }
        }, 600);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(5.66, 34.26, 16.79);
            }
        }, 900);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(10.2, 55.78, 25.34);
            }
        }, 1200);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(14.35, 59.65, 21.91);
            }
        }, 1500);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(17.35, 66.65, 30.71);
            }
        }, 1800);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(16.35, 79.55, 37.11);
            }
        }, 2100);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(21.37, 121.22, 50.93);
            }
        }, 240000);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(22.37, 131.22, 52.58);
            }
        }, 2700);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(24.37, 151.22, 55.93);
            }
        }, 3000);

    }

    public void setData(Double first, Double sec, Double third) {
        if (null != xtNetDelayTime) {
            xtNetDelayTime.setText("sBig" + first + "eBigms");
        }
        if (null != xtDownloadSpeed) {
            xtDownloadSpeed.setText("sBig" + sec + "eBigmMB/s");
        }
        if (null != xtUploadSpeed) {
            xtUploadSpeed.setText("sBig" + third + "eBigMB/s");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void getAdTypeInfo() {
        RxDataInstance.getInstance().initMap(NetworkTestSpeedGifActivity.this)
                .put("entryNumber", "4");
        HttpManager.getInstance().getApi().getAdTypeInfo(RxDataInstance.getInstance().getParamMap())
                .compose(RxUtil.<AdTypeInfoResult>bindLifecycleAndApplySchedulers(this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseDataSubscriber<AdTypeInfoResult>(new HttpErrorHandler() {
                    @Override
                    public void onHttpError(BaseResult result) {
                        ToastUtils.showShortToast(NetworkTestSpeedGifActivity.this, result.getErrorMessage());
                    }

                    @Override
                    public void onHttpException(Throwable e) {
                        // dismissLoadingDialog();
                    }
                }) {
                    @Override
                    public void onDataNext(AdTypeInfoResult result) {
                        int resultCode = result.getResultCode();
                        if (0 == resultCode) {
                            AdTypeInfoModel data = result.getData();
                            if (null != data) {
                                type = data.getType();
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (type == 1) {
                                            newInsertFullAd.showNewInterAd();
                                            newInsertFullAd.setItemAllClickListener(new LoadCsjNewInsertFullAdUtils.OnItemAllClickListener() {
                                                @Override
                                                public void onItemAllClickListener() {
                                                    jumpNext();
                                                }
                                            });
                                        } else if (type == 2) {
                                            loadYLHJiliVideoUtils.play();
                                            loadYLHJiliVideoUtils.setAdCloseListener(new LoadYLHJiliVideoUtils.OnAdCloseListener() {
                                                @Override
                                                public void onAdCloseListenerListener(boolean isPlayComplete) {
                                                    jumpNext();
                                                }
                                            });
                                        }
                                    }
                                }, 3000);
                            }
                        } else {
                            ToastUtils.showShortToast(NetworkTestSpeedGifActivity.this, result.getErrorMessage());
                        }
                    }
                });
    }

    private void playErrorRepalceAd(int type) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (type == 1) {
                    newInsertFullAd.showNewInterAd();
                    newInsertFullAd.setItemAllClickListener(new LoadCsjNewInsertFullAdUtils.OnItemAllClickListener() {
                        @Override
                        public void onItemAllClickListener() {
                            jumpNext();
                        }
                    });
                } else {
                    loadYLHJiliVideoUtils.play();
                    loadYLHJiliVideoUtils.setAdCloseListener(new LoadYLHJiliVideoUtils.OnAdCloseListener() {
                        @Override
                        public void onAdCloseListenerListener(boolean isPlayComplete) {
                            jumpNext();
                        }
                    });
                }
            }
        }, 1000);
    }

    private void jumpNext() {
        ActivityAllManager.getInstance().finishActivity(NetworkTestSpeedGifActivity.class);
        Skip.toActivity(NetworkTestSpeedGifActivity.this, NetworkTestSpeedActivity.class);
    }

}
